package kernel

import (
	"awesomeTask/application/variable"
	"fmt"
	"github.com/shirou/gopsutil/cpu"
	"github.com/shirou/gopsutil/mem"
	"net"
	exec2 "os/exec"
	"runtime"
	"time"
)

type Kernel interface {
	GetNodeInfo()                                   //获取当前节点信息
	GetNodeRunningTask() int                        //获取当前节点正在运行的节点
	GetNodeMaxRunTask()                             //获取当前节点最大运行节点数
	GetNodeCpuUseAndMemory() map[string]interface{} //获取当前节点的cpu使用率以及内存使用率
	ExceShellOrBash(command string, isAysn bool) string
	ExceShellOrBashAysn(command string) *exec2.Cmd //异步的执行任务
	GetMac() string
}
type WindowsDriver struct {
}
type LinuxsDriver struct {
}

/**
获取当前系统剩余的资源
*/
func (driver WindowsDriver) GetNodeCpuUseAndMemory() map[string]interface{} {
	info := map[string]interface{}{}
	memInfo, _ := mem.VirtualMemory()
	info["totalMemory"] = memInfo.Total / (1024 * 1024)
	info["freeMemory"] = memInfo.Available / (1024 * 1024)
	info["usedMemory"] = memInfo.Used / (1024 * 1024)
	info["cpu"] = runtime.NumCPU()
	percent, _ := cpu.Percent(time.Second, false)
	info["cpuUsed"] = percent[0]
	info["currentRun"] = driver.GetNodeRunningTask()
	return info
}
func (driver WindowsDriver) GetMac() string {
	mac := ""
	interfaces, err := net.Interfaces()
	if err != nil {
		panic("Poor soul, here is what you got: " + err.Error())
	}
	for _, inter := range interfaces {
		mac = inter.HardwareAddr.String() //获取本机MAC地址
		break
	}
	return mac
}
func (dirver WindowsDriver) GetNodeInfo() {

}
func (driver WindowsDriver) GetNodeRunningTask() int {
	return variable.CurrentRunningTaskNum
}
func (driver WindowsDriver) GetNodeMaxRunTask() {

}
func (driver WindowsDriver) ExceShellOrBash(command string, isAysn bool) string {
	return exec(command, isAysn)
}
func (driver WindowsDriver) ExceShellOrBashAysn(command string) *exec2.Cmd {
	var c *exec2.Cmd
	if IsLinux() {
		c = exec2.Command("bash", "-c", command)
	} else {
		c = exec2.Command("cmd", "/C", command)
	}

	// 此处是windows版本

	return c
}
func (driver LinuxsDriver) ExceShellOrBashAysn(command string) *exec2.Cmd {
	var c *exec2.Cmd
	if IsLinux() {
		c = exec2.Command("bash", "-c", command)
	} else {
		c = exec2.Command("cmd", "/C", command)
	}

	// 此处是windows版本

	return c
}
func (driver LinuxsDriver) GetNodeCpuUseAndMemory() map[string]interface{} {
	info := map[string]interface{}{}
	memInfo, _ := mem.VirtualMemory()
	info["totalMemory"] = memInfo.Total / (1024 * 1024)
	info["freeMemory"] = memInfo.Available / (1024 * 1024)
	info["usedMemory"] = memInfo.Used / (1024 * 1024)
	info["cpu"] = runtime.NumCPU()
	percent, _ := cpu.Percent(time.Second, false)
	info["cpuUsed"] = percent[0]
	info["currentRun"] = driver.GetNodeRunningTask()
	return info
}
func (driver LinuxsDriver) GetNodeRunningTask() int {
	return variable.CurrentRunningTaskNum
}
func (dirver LinuxsDriver) GetNodeInfo() {

}
func (driver LinuxsDriver) GetNodeMaxRunTask() {

}
func (driver LinuxsDriver) ExceShellOrBash(command string, isAysn bool) string {
	return exec(command, isAysn)
}
func (driver LinuxsDriver) GetMac() string {
	mac := ""
	interfaces, err := net.Interfaces()
	if err != nil {
		panic("Poor soul, here is what you got: " + err.Error())
	}
	for _, inter := range interfaces {
		mac = inter.HardwareAddr.String() //获取本机MAC地址
	}
	return mac
}

/**
执行系统调用
*/
func exec(command string, isAysn bool) string {
	var c *exec2.Cmd
	if IsLinux() {
		c = exec2.Command("bash", "-c", command)
	} else {
		c = exec2.Command("cmd", "/C", command)
	}

	// 此处是windows版本

	output, err := c.CombinedOutput()
	if err != nil {
		fmt.Println(err.Error(), "系统调用失败==>")
		fmt.Println(string(output))
		return err.Error()
	}
	return string(output)
}

/**
判断当前是window还是linux系统
*/
func IsLinux() bool {
	sysType := runtime.GOOS

	if sysType == "linux" {
		// LINUX系统
		return true
	}

	if sysType == "windows" {
		// windows系统
		return false
	}
	return false
}
func GetKernelDriver() Kernel {
	if IsLinux() {
		return LinuxsDriver{}
	}
	return WindowsDriver{}
}
